from flask import Flask, render_template,request,jsonify,make_response
from spiderplus import getmtjmdata,get_mtjm
import json

app = Flask(__name__)

@app.route('/', methods=['GET','POST'])
def Login():
    if request.method == 'GET':
        return render_template('Login.html')
    if request.method == 'POST':
        data = request.data.decode('utf-8')
        data = eval(data)
        if data['username']=='admin' and data['password']=='admin':
            return {'msg':'登陆成功'}
        else:
            return {'msg':'用户名或密码错误！'}
@app.route('/index')
def index():
    return render_template('lt.html')


@app.route('/work3_25')
def work3_25():
    return render_template('work3_25.html')
@app.route('/work3_25plus')
def work3_25plus():
    return render_template('work3_25plus.html')

@app.route('/work4_1')
def work4_1():
    return render_template('work4_1.html')

@app.route('/store')
def store():
    return render_template('store.html')

@app.route('/layui')
def layui():
    return render_template('layui.html')


def add(data):
    with open('student.json','w') as f:
        f.write(str(data))

student = []
@app.route('/ajax',methods = ['get','post'])
def ajax():
    if request.method == 'GET':
        return render_template('ajax.html')
    if request.method == 'POST':
        data = request.data.decode('utf-8')
        student.append(data)
        print(student)
        try:
            add(student)
        except:
            return 'no'
        return jsonify(data)
@app.route('/getdata',methods = ['get','post'])
def getdata():
    return student


@app.route('/spider',methods = ['get','post'])
def spider():
    import requests

    url = "http://www.hceb.edu.cn/xwzx/xxxw.htm"
    response = requests.get(url)
    response.encoding = 'utf-8'

    r = ''

    if response.status_code == 200:
        r = response.text

    return render_template('spider.html',msg=r)
@app.route('/spiderplus')
def spiderplus():
    return render_template('spiderplus.html')

@app.route('/api/spider')
def api_spiderplus():
    data =get_mtjm()
    return jsonify(data)

@app.route('/wzry')
def wzry():
    return render_template('wzry.html')

@app.route('/cd')
def cd():
    return render_template('cd.html')

if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0',port=80)

